package com.example.xln111.controller;

import com.example.xln111.model.Grade;
import com.example.xln111.service.GradeService;
import com.example.xln111.util.GeneralResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RestController
public class GradeController {
        @Autowired
        private GradeService gradeService;

        @GetMapping("/grades")
        public GeneralResponse grades() {
            GeneralResponse response = new GeneralResponse();
            response.setSuccess(true);
            response.setData(gradeService.getAllGrades());
            return response;
        }

        @PostMapping("/grade/add")
        public GeneralResponse add(@RequestParam Integer studentId,@RequestParam Integer courseId, @RequestParam String time, @RequestParam Integer score){
            Grade grade =new Grade();
            grade.setStudentId(studentId);
            grade.setCourseId(courseId);
            grade.setTime(time);
            grade.setScore(score);

            GeneralResponse response = new GeneralResponse();
            response.setSuccess(true);
            response.setData(gradeService.add(grade));
            return response;
        }

        @PutMapping("/grade/update")
        public GeneralResponse update(@RequestParam Integer courseId,@RequestParam String time,@RequestParam Integer score,@RequestParam  int studentId){
            Grade grade =new Grade();
            grade.setStudentId(studentId);
            grade.setTime(time);
            grade.setCourseId(courseId);
            grade.setScore(score);

            GeneralResponse response = new GeneralResponse();
            response.setSuccess(true);
            response.setData(gradeService.update(grade));
            return response;
        }

        @PostMapping("/grade/delete")
        public GeneralResponse delete(@RequestParam int studentId){
            GeneralResponse response = new GeneralResponse();
            response.setSuccess(true);
            response.setData(gradeService.deleteById(studentId));

            return response;
        }


        @GetMapping("/grade")
        public GeneralResponse gradeById(@RequestParam int studentId){
            GeneralResponse response = new GeneralResponse();
            response.setSuccess(true);
            List<Grade> result = new ArrayList<>();
            result.add(gradeService.getById(studentId));
            response.setData(result);
            return response;
        }
    }

